﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KongZhiTaiTest
{
    /// <summary>
    /// 有关数据结构的相关内容
    /// </summary>
    public class DataStruct
    {

        /// <summary>
        /// 给数组扩容
        /// </summary>
        /// <param name="source"></param>
        /// <param name="newLength"></param>
        public void Reset(int[] source, int newLength)
        {
            int[] newBuckets = new int[newLength];
            Array.Copy(source, 0, newBuckets, 0, source.Length);
            source = newBuckets;
        }


        public void AllCSharpCollection()
        {
            //数据结构：链表
            LinkedList<int> linkList = new LinkedList<int>();
            //数据结构：数组
            ArrayList arrayList = new ArrayList();
            //数据结构：数组
            List<int> list = new List<int>();  
            //数据结构：内部存储两个数组int[] Entry[]（一个用来存储首次查找的index ,Entry[]用来存储真正的数据对象，包含4个值（key,value,hashCode,next））
            Dictionary<int, object> dic = new Dictionary<int, object>();

            //数据结构：与Dictionary 非常相似没有重复的值，只不过没有key的泛型，
            //内部依然使用两个数组 ：int[] Slot[]  int[]用来存储Slot[]的索引（下标） ，Slot是一个Struct里面有三个字段（value ,hashCode,next）
            HashSet<int> hashSet = new HashSet<int>(); 
            //队列：数组 主要的内部结构：array,head,tail,size 
            Queue<int> queue = new Queue<int>(10);
            queue.Dequeue(); //出队列,通过 _array[_head] = default(T);清理对应的索引下的数据 ，_head = 
            queue.Enqueue(33);//入队操作,通过_array[tail] = 33; tail = (tail+1)%lenght (因为是循环队列)


            //栈：数组 
            //后入先出
            Stack<int> stack = new Stack<int>(5);
            stack.Push(10);
            stack.Push(20);
            stack.Push(30);
            stack.Push(40);
            stack.Push(50);
            int first = stack.Count;
            Console.WriteLine(first);
            stack.Pop();
            int second = stack.Count;
            Console.WriteLine(second);
        }

        public void StackTest()
        {
            Stack<int> stack = new Stack<int>(5);
            stack.Push(10);
            stack.Push(20);
            stack.Push(30);
            stack.Push(40);
            stack.Push(50);
            int first = stack.Count;
            Console.WriteLine(first);
            StringBuilder sb = new StringBuilder();
            foreach (int item in stack)
            {
                sb.Append(item + ",");
            }
            string str = sb.ToString().Substring(0, sb.Length - 1);
            str = "["+str+"]";
            Console.WriteLine(str);
            Console.WriteLine();
            stack.Pop();
            sb.Clear();
            foreach (int item in stack)
            {
                sb.Append(item + ",");
            }
            str = sb.ToString().Substring(0, sb.Length - 1);
            str = "[" + str + "]";
            Console.WriteLine(str);
            int second = stack.Count;
            Console.WriteLine(second);
        }
    }
}
